<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Base_lib
 *
 * @author Administrator
 */
class Base_lib {

  private $table_name;
  private $ci;

  public function __construct($table = '') {
    $this->table_name = $table;
    $this->ci = & get_instance();
  }

  public function check_exist($value, $field, $id = '') {
    if (is_array($value) && is_array($field)) {
      foreach ($value as $i => $v) {
        $this->ci->db->where($field[$i], $v);
      }
    } else {
      $this->ci->db->where($field, $value);
    }
    if (!empty($id)) {
      $this->ci->db->where('id != ', $id);
    }
    $a = $this->ci->db->count_all_results($this->table_name);
    if (!empty($a)) {
      return false;
    }
    return true;
  }

  function _upload_img($params, $folder, $get_all_data = false) {
    if (!file_exists($this->ci->setting_model->item('upload_path') . $folder)) {
      @mkdir($this->ci->setting_model->item('upload_path') . $folder, 0777);
    }
    $config['upload_path'] = $this->ci->setting_model->item('upload_path') . $folder;
    $config['allowed_types'] = $this->ci->setting_model->item('allowed_types_image');
    $config['encrypt_name'] = $this->ci->setting_model->item('encrypt_name_upload');
    $this->ci->load->library('upload');
    $this->ci->upload->initialize($config);
    if ($this->ci->upload->do_upload($params)) {
      $img = $this->ci->upload->data();
      if ($get_all_data) {
        return $img;
      } else {
        return $img['file_name'];
      }
    }
    return '';
  }

  function _upload_file($params, $folder, $get_all_data = false) {
    if (!file_exists($this->ci->setting_model->item('upload_path') . $folder)) {
      @mkdir($this->ci->setting_model->item('upload_path') . $folder, 0777);
    }
    $config['upload_path'] = $this->ci->setting_model->item('upload_path') . $folder;
    $config['allowed_types'] = $this->ci->setting_model->item('allowed_types_file');
    $config['encrypt_name'] = $this->ci->setting_model->item('encrypt_name_upload');
    $this->ci->load->library('upload');
    $this->ci->upload->initialize($config);
    if ($this->ci->upload->do_upload($params)) {
      $img = $this->ci->upload->data();
      if ($get_all_data) {
        return $img;
      } else {
        return $img['file_name'];
      }
    }
    return '';
  }

  function multiple_upload($param, $folder) {
    $files = array();
    if (!file_exists($this->ci->setting_model->item('upload_path') . $folder)) {
      @mkdir($this->ci->setting_model->item('upload_path') . $folder, 0777);
    }
    $config['upload_path'] = $this->ci->setting_model->item('upload_path') . $folder;
    $config['allowed_types'] = $this->ci->setting_model->item('allowed_types_file');
    $config['encrypt_name'] = $this->ci->setting_model->item('encrypt_name_upload');
    $this->ci->load->library('upload');
    $this->ci->upload->initialize($config);
    if (!empty($_FILES[$param])) {
      for ($k = 0; $k < count($_FILES[$param]['name']); $k++) {
        if ($this->ci->upload->do_upload($param, $k)) {
          $img = $this->ci->upload->data();
          $files[$_FILES[$param]['name'][$k]] = $img['file_name'];
        }
      }
      return $files;
    }
    return array();
  }

  function get_img_from_path($path = '') {
    if (empty($path)) {
      header('Content-type: image');
      readfile($this->ci->setting_model->item('upload_path') . 'noimage.png');
      exit;
    }
    $path = str_replace('.html', '', $path);
    $msearch = "_jpg";
    $mrep = ".jpg";
    $moff = strlen($path) - 5;
    $path = str_replace($msearch, $mrep, $path, $moff);
    header('Content-type: image');
    if (is_file($path)) {
      readfile($path);
    } else {
      readfile($this->ci->setting_model->item('upload_path') . 'noimage.png');
    }
    exit;
  }

}